package ui.panel
{

	import flash.display.Bitmap;

	import globals.ButtonType;
	import globals.Global;

	import manager.PopUpManager;

	import net.data.EquipItemInfo;
	import net.data.EquipListOnHero;
	import net.data.HeroInfo;
	import net.message.CG_Equip_ShowAvailableMessage;
	import net.message.CG_Loading_HeroMessage;

	import ui.component.Grid;
	import ui.component.Page;
	import ui.component.TextButton;
	import ui.panel.common.HeroList;
	import ui.panel.equips.EquipWarriorInfo;

	import util.Long;
	import util.UIUtil;

	/**
	 * 装备
	 * @author luguobin
	 */
	public class EquipPanel extends BasePanel
	{
		private var _warriorInfo:EquipWarriorInfo;
		private var _toolItems:Array = []; //装备
		private var _tabBtns:Array = [];
		private var _page:Page;
		private var _heroList:HeroList; //弹出的英雄列表
		private var _curHeroEquipList:Vector.<EquipListOnHero>;
		private var _equipData:Vector.<EquipItemInfo>;
		private var _curHeroId:Long;

		public function EquipPanel()
		{
			super();
			pid = PanelType.EQUIP_PANEL;
			updateTitle("装备");
		}

		override protected function initUI():void
		{
			super.initUI();
			//背景
			var contentBg:Bitmap = new Bitmap(Global.assetsManager.getAssetsAsBitmapData("content_bg"));
			contentBg.x = 27;
			contentBg.y = 38;
			this.addChild(contentBg);

			_warriorInfo = new EquipWarriorInfo();
			_warriorInfo.x = 60;
			_warriorInfo.y = 70;
			this.addChild(_warriorInfo);

			/*var tab:TextButton;
			   tab = UIUtil.getTextButton(0, "武器", ButtonType.BUTTON_MENU_TAB, tabHandler);
			   tab.setIcon(Global.assetsManager.getAssetsAsBitmapData("tab_on"));
			   tab.x = 200;
			   tab.y = 44;
			   tab.getLabel().y = 12;
			   _tabBtns.push(tab);
			   this.addChild(tab);

			   tab = UIUtil.getTextButton(1, "饰品", ButtonType.BUTTON_MENU_TAB, tabHandler);
			   tab.x = 280;
			   tab.y = 44;
			   tab.getLabel().y = 12;
			   _tabBtns.push(tab);
			   this.addChild(tab);

			   tab = UIUtil.getTextButton(2, "道具", ButtonType.BUTTON_MENU_TAB, tabHandler);
			   tab.x = 360;
			   tab.y = 44;
			   tab.getLabel().y = 12;
			   _tabBtns.push(tab);
			 this.addChild(tab);*/
			var gap:int = Grid.SIZE + 16;

			for(var i:int = 0; i < 16; i++)
			{
				var grid:Grid = new Grid();
				grid.type = PanelType.TOWER_PANEL;
				grid.index = i;
				grid.clickCallback = gridHandler;
				grid.x = i % 4 * gap + 235;
				grid.y = int(i / 4) * gap + 70;
				this.addChild(grid);
				_toolItems.push(grid);
			}

			_page = new Page(pageHandler);
			_page.x = 330;
			_page.y = 395;
			this.addChild(_page);

			_heroList = new HeroList();
			_heroList.selectCallback = selectHero;
		}

		/*private function tabHandler(button:TextButton):void
		   {
		   var id:uint = button.id;

		   for(var i:int = 0; i < _tabBtns.length; i++)
		   {
		   var tab:TextButton = _tabBtns[i];

		   if(tab == button)
		   {
		   tab.setIcon(Global.assetsManager.getAssetsAsBitmapData("tab_on"));
		   tab.getLabel().y = 10;
		   }
		   else
		   {
		   tab.setIcon(Global.assetsManager.getAssetsAsBitmapData("tab_off"));
		   tab.getLabel().y = 12;
		   }
		   }
		 }*/

		private function pageHandler(num:uint):void
		{
			for(var i:int = 0; i < 16; i++)
			{
				var curIndex:int = (num - 1) * 16 + i;
				var grid:Grid = _toolItems[i];

				if(curIndex < _equipData.length)
				{
					grid.update(_equipData[curIndex]);
				}
				else
				{
					grid.clear();
				}
			}
		}

		private function gridHandler(type:uint, index:uint):void
		{

		}

		public function getEquipData(heroId:Long = null):void
		{
			_curHeroId = heroId;
			preGetEquip();
		}

		/**先获取英雄列表**/
		private function preGetEquip():void
		{
			CG_Loading_HeroMessage.listType = CG_Loading_HeroMessage.EQUIP_UI;
			var loading_hero:CG_Loading_HeroMessage = new CG_Loading_HeroMessage();
			loading_hero.writeMessage();
		}

		public function heroListData(data:Vector.<HeroInfo>):void
		{
			if(_curHeroId == null)
			{
				_curHeroId = (data[0] as HeroInfo).heroId;
			}
			getCurHeroEquip(_curHeroId);
			//赋值给弹出的英雄列表赋值。
			_heroList.update(data);
		}

		/**获取当前的英雄装备列表信息**/
		private function getCurHeroEquip(heroId:Long):void
		{
			var cgMsg:CG_Equip_ShowAvailableMessage = new CG_Equip_ShowAvailableMessage();
			cgMsg.writeMessage(heroId);
		}

		public function update(curHeroEquipList:Vector.<EquipListOnHero>, equipProvider:Vector.<EquipItemInfo>):void
		{
			_curHeroEquipList = curHeroEquipList;
			_equipData = equipProvider;
			var len:int = equipProvider.length;
			_page.max = int(len / _toolItems.length) + (len % _toolItems.length > 0 ? 1 : 0);
			pageHandler(1);
		}

		private function selectHero(heroId:Long):void
		{
			PopUpManager.removePopUp(_heroList);
			getCurHeroEquip(heroId);
		}

		public function popUpHeroList():void
		{
			_heroList.x = this.x + 200;
			_heroList.y = this.y + 65;
			PopUpManager.addPopUp(_heroList, true);
		}
	}
}